package com.example.gray.arcview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class ArcView extends View {
    private final float startAngle = 135;
    private final float sweepAngle = 270;
    private final int defWidth = 500;
    private final int defHeight = 500;

    private Context mContext;
    private Paint mArcPaint;
    private Paint mProgressPaint;
    private Paint mTextPaint;

    private float mCenterX;
    private float mCenterY;
    private float radius;                   //半径

    private int mRoundColor;                //外层圆弧的颜色
    private int mProgressColor;             //进度的颜色
    private float mRoundWidth;              //圆弧宽度
    private int mTextColor;                 //文字的颜色
    private float mTextSize;                //文字的尺寸

    private RectF ovalRectF = new RectF();  //圆弧的外接矩形
    private int progress;                   //进度
    private int max;                        //最大值
    private float percent;                  //进度百分比
    Rect textRect = new Rect();

    public ArcView(Context context) {
        this(context, null);
    }

    public ArcView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ArcView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initAttrs(attrs);
        initPaint();
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.ArcView);
        mRoundColor = typedArray.getColor(R.styleable.ArcView_roundColor,
                ContextCompat.getColor(mContext, R.color.colorPrimary));
        mProgressColor = typedArray.getColor(R.styleable.ArcView_progressColor,
                ContextCompat.getColor(mContext, R.color.colorAccent));
        mTextColor = typedArray.getColor(R.styleable.ArcView_textColor,
                ContextCompat.getColor(mContext, R.color.colorAccent));
        mRoundWidth = typedArray.getDimension(R.styleable.ArcView_roundWidth, 15);
        mTextSize = typedArray.getDimension(R.styleable.ArcView_textSize, 24);
        typedArray.recycle();
    }

    private void initPaint() {
        //初始化外层圆弧画笔
        mArcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mArcPaint.setStyle(Paint.Style.STROKE);
        mArcPaint.setStrokeCap(Paint.Cap.ROUND);
        mArcPaint.setStrokeJoin(Paint.Join.ROUND);
        mArcPaint.setColor(mRoundColor);
        mArcPaint.setStrokeWidth(mRoundWidth);
        //初始化进度画笔
        mProgressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mProgressPaint.setStyle(Paint.Style.STROKE);
        mProgressPaint.setStrokeCap(Paint.Cap.ROUND);
        mProgressPaint.setStrokeJoin(Paint.Join.ROUND);
        mProgressPaint.setColor(mProgressColor);
        mProgressPaint.setStrokeWidth(mRoundWidth);
        //初始化文字画笔
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setColor(mTextColor);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);   //获取宽的模式
        int heightMode = MeasureSpec.getMode(heightMeasureSpec); //获取高的模式
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);   //获取宽的尺寸
        int heightSize = MeasureSpec.getSize(heightMeasureSpec); //获取高的尺寸
        Log.d("liu", "宽的模式:" + widthMode);
        Log.d("liu", "高的模式:" + heightMode);
        Log.d("liu", "宽的尺寸:" + widthSize);
        Log.d("liu", "高的尺寸:" + heightSize);
        int width=resolveSize(defWidth,widthMeasureSpec);
        int height =resolveSize(defHeight,heightMeasureSpec);
        Log.d("liu", "测量后宽的尺寸:" + width);
        Log.d("liu", "测量后高的尺寸:" + height);
        int min=Math.min(width,height);
        setMeasuredDimension(min, min);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initParam();
        //画出最外层圆弧
        canvas.drawArc(ovalRectF, startAngle, sweepAngle, false, mArcPaint);
        //画出进度
        percent = (float) progress / max;
        canvas.drawArc(ovalRectF, startAngle, percent * sweepAngle, false, mProgressPaint);
        //绘制文字
        String text = progress + "";
        mTextPaint.getTextBounds(text, 0, text.length(), textRect);
        float dx = mCenterX - textRect.width() / 2;
        // 获取画笔的FontMetrics
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        // 计算文字的基线
        float baseLine = mCenterY + ((fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom);
        canvas.drawText(text, dx, baseLine, mTextPaint);
    }

    /**
     * 初始化参数
     */
    private void initParam() {
        int left = getPaddingLeft();
        int top = getPaddingTop();
        int right = getMeasuredWidth() - getPaddingRight();
        int bottom = getMeasuredHeight() - getPaddingBottom();

        int circleWidth=right-left;
        int circleHeight=bottom-top;
        int min = Math.min(circleWidth, circleHeight);
        //计算圆心坐标
        mCenterX = (float) min/2+getPaddingLeft();
        mCenterY = (float) min/2+getPaddingTop();
        //计算半径
        if (mCenterX>mCenterY){
            radius=mCenterY-mRoundWidth-getPaddingTop();
        }else {
            radius=mCenterX-mRoundWidth-getPaddingLeft();
        }
        Log.d("liu", "圆心横坐标:" + mCenterX);
        Log.d("liu", "圆心纵坐标:" + mCenterY);
        Log.d("liu", "半径:" + radius);
        Log.d("liu", "画笔的宽度:" + mRoundWidth);
        ovalRectF.set(mCenterX-radius, mCenterY-radius, mCenterX+radius, mCenterY+radius);
    }

    public void setMax(int max) {
        if (max < 0) {
            throw new IllegalArgumentException("max 不能小于0!");
        }
        this.max = max;
    }

    public void setProgress(int progress) {
        if (progress < 0) {
            throw new IllegalArgumentException("progress 不能小于0!");
        }
        this.progress = progress;
        invalidate();
    }

    public int getProgress() {
        return progress;
    }

}


